
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>驱动和块设备驱动 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part-4.html" />
    
    
    <link rel="prev" href="part-2.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../../notes/log.html">
            
                <a href="../../../notes/log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">实验之前</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../pre-lab/env.html">
            
                <a href="../../pre-lab/env.html">
            
                    
                    环境部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../pre-lab/gdb.html">
            
                <a href="../../pre-lab/gdb.html">
            
                    
                    GDB 调试方法*
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">实验指导</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    实验指导零
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../lab-0/guide/intro.html">
            
                <a href="../../lab-0/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../lab-0/guide/part-1.html">
            
                <a href="../../lab-0/guide/part-1.html">
            
                    
                    创建项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../lab-0/guide/part-2.html">
            
                <a href="../../lab-0/guide/part-2.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../../lab-0/guide/part-3.html">
            
                <a href="../../lab-0/guide/part-3.html">
            
                    
                    移除运行时环境依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../../lab-0/guide/part-4.html">
            
                <a href="../../lab-0/guide/part-4.html">
            
                    
                    编译为裸机目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../../lab-0/guide/part-5.html">
            
                <a href="../../lab-0/guide/part-5.html">
            
                    
                    生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="../../lab-0/guide/part-6.html">
            
                <a href="../../lab-0/guide/part-6.html">
            
                    
                    调整内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="../../lab-0/guide/part-7.html">
            
                <a href="../../lab-0/guide/part-7.html">
            
                    
                    重写程序入口点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.9" data-path="../../lab-0/guide/part-8.html">
            
                <a href="../../lab-0/guide/part-8.html">
            
                    
                    使用 QEMU 运行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.10" data-path="../../lab-0/guide/part-9.html">
            
                <a href="../../lab-0/guide/part-9.html">
            
                    
                    接口封装和代码整理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.11" data-path="../../lab-0/guide/summary.html">
            
                <a href="../../lab-0/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" >
            
                <span>
            
                    
                    实验指导一
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../../lab-1/guide/intro.html">
            
                <a href="../../lab-1/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="../../lab-1/guide/part-1.html">
            
                <a href="../../lab-1/guide/part-1.html">
            
                    
                    什么是中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="../../lab-1/guide/part-2.html">
            
                <a href="../../lab-1/guide/part-2.html">
            
                    
                    RISC-V 中的中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="../../lab-1/guide/part-3.html">
            
                <a href="../../lab-1/guide/part-3.html">
            
                    
                    程序运行状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.5" data-path="../../lab-1/guide/part-4.html">
            
                <a href="../../lab-1/guide/part-4.html">
            
                    
                    状态的保存与恢复
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.6" data-path="../../lab-1/guide/part-5.html">
            
                <a href="../../lab-1/guide/part-5.html">
            
                    
                    进入中断处理流程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.7" data-path="../../lab-1/guide/part-6.html">
            
                <a href="../../lab-1/guide/part-6.html">
            
                    
                    时钟中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.8" data-path="../../lab-1/guide/summary.html">
            
                <a href="../../lab-1/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" >
            
                <span>
            
                    
                    实验指导二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="../../lab-2/guide/intro.html">
            
                <a href="../../lab-2/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="../../lab-2/guide/part-1.html">
            
                <a href="../../lab-2/guide/part-1.html">
            
                    
                    动态内存分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="../../lab-2/guide/part-2.html">
            
                <a href="../../lab-2/guide/part-2.html">
            
                    
                    物理内存探测
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="../../lab-2/guide/part-3.html">
            
                <a href="../../lab-2/guide/part-3.html">
            
                    
                    物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="../../lab-2/guide/summary.html">
            
                <a href="../../lab-2/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" >
            
                <span>
            
                    
                    实验指导三
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="../../lab-3/guide/intro.html">
            
                <a href="../../lab-3/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="../../lab-3/guide/part-1.html">
            
                <a href="../../lab-3/guide/part-1.html">
            
                    
                    从虚拟地址到物理地址
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="../../lab-3/guide/part-2.html">
            
                <a href="../../lab-3/guide/part-2.html">
            
                    
                    修改内核
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="../../lab-3/guide/part-3.html">
            
                <a href="../../lab-3/guide/part-3.html">
            
                    
                    实现页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="../../lab-3/guide/part-4.html">
            
                <a href="../../lab-3/guide/part-4.html">
            
                    
                    实现内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.6" data-path="../../lab-3/guide/part-5.html">
            
                <a href="../../lab-3/guide/part-5.html">
            
                    
                    页面置换*
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.7" data-path="../../lab-3/guide/summary.html">
            
                <a href="../../lab-3/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" >
            
                <span>
            
                    
                    实验指导四
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="../../lab-4/guide/intro.html">
            
                <a href="../../lab-4/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.2" data-path="../../lab-4/guide/part-1.html">
            
                <a href="../../lab-4/guide/part-1.html">
            
                    
                    线程和进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.3" data-path="../../lab-4/guide/part-2.html">
            
                <a href="../../lab-4/guide/part-2.html">
            
                    
                    线程的创建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.4" data-path="../../lab-4/guide/part-3.html">
            
                <a href="../../lab-4/guide/part-3.html">
            
                    
                    线程的切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.5" data-path="../../lab-4/guide/part-4.html">
            
                <a href="../../lab-4/guide/part-4.html">
            
                    
                    线程的结束
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.6" data-path="../../lab-4/guide/part-5.html">
            
                <a href="../../lab-4/guide/part-5.html">
            
                    
                    内核栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.7" data-path="../../lab-4/guide/part-6.html">
            
                <a href="../../lab-4/guide/part-6.html">
            
                    
                    线程调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.8" data-path="../../lab-4/guide/summary.html">
            
                <a href="../../lab-4/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" >
            
                <span>
            
                    
                    实验指导五
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="intro.html">
            
                <a href="intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.2" data-path="part-1.html">
            
                <a href="part-1.html">
            
                    
                    设备树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.3" data-path="part-2.html">
            
                <a href="part-2.html">
            
                    
                    virtio
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.6.4" data-path="part-3.html">
            
                <a href="part-3.html">
            
                    
                    驱动和块设备驱动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.5" data-path="part-4.html">
            
                <a href="part-4.html">
            
                    
                    文件系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.6" data-path="summary.html">
            
                <a href="summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" >
            
                <span>
            
                    
                    实验指导六
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.7.1" data-path="../../lab-6/guide/intro.html">
            
                <a href="../../lab-6/guide/intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.2" data-path="../../lab-6/guide/part-1.html">
            
                <a href="../../lab-6/guide/part-1.html">
            
                    
                    构建用户程序框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.3" data-path="../../lab-6/guide/part-2.html">
            
                <a href="../../lab-6/guide/part-2.html">
            
                    
                    打包为磁盘镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.4" data-path="../../lab-6/guide/part-3.html">
            
                <a href="../../lab-6/guide/part-3.html">
            
                    
                    解析 ELF 文件并创建线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.5" data-path="../../lab-6/guide/part-4.html">
            
                <a href="../../lab-6/guide/part-4.html">
            
                    
                    实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.6" data-path="../../lab-6/guide/part-5.html">
            
                <a href="../../lab-6/guide/part-5.html">
            
                    
                    处理文件描述符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.7" data-path="../../lab-6/guide/part-6.html">
            
                <a href="../../lab-6/guide/part-6.html">
            
                    
                    条件变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.8" data-path="../../lab-6/guide/summary.html">
            
                <a href="../../lab-6/guide/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">实验题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../lab-1/practice.html">
            
                <a href="../../lab-1/practice.html">
            
                    
                    实验一：中断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../lab-2/practice.html">
            
                <a href="../../lab-2/practice.html">
            
                    
                    实验二：内存分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../../lab-3/practice.html">
            
                <a href="../../lab-3/practice.html">
            
                    
                    实验三：虚实地址转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../../lab-4/practice-1.html">
            
                <a href="../../lab-4/practice-1.html">
            
                    
                    实验四（上）：线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../../lab-4/practice-2.html">
            
                <a href="../../lab-4/practice-2.html">
            
                    
                    实验四（下）：线程调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="../../lab-6/practice.html">
            
                <a href="../../lab-6/practice.html">
            
                    
                    实验六：系统调用
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">开发笔记</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../format/partition.html">
            
                <a href="../../format/partition.html">
            
                    
                    文档代码划分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../../format/doc.html">
            
                <a href="../../format/doc.html">
            
                    
                    文档格式规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../../format/code.html">
            
                <a href="../../format/code.html">
            
                    
                    代码格式规范
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >驱动和块设备驱动</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x9A71;&#x52A8;&#x548C;&#x5757;&#x8BBE;&#x5907;&#x9A71;&#x52A8;">&#x9A71;&#x52A8;&#x548C;&#x5757;&#x8BBE;&#x5907;&#x9A71;&#x52A8;</h2>
<h3 id="&#x4EC0;&#x4E48;&#x662F;&#x5757;&#x8BBE;&#x5907;">&#x4EC0;&#x4E48;&#x662F;&#x5757;&#x8BBE;&#x5907;</h3>
<p>&#x6CE8;&#x610F;&#x5230;&#x6211;&#x4EEC;&#x5728;&#x4ECB;&#x7ECD; virtio &#x65F6;&#x63D0;&#x5230;&#x4E86; virtio-blk &#x8BBE;&#x5907;&#xFF0C;&#x8FD9;&#x79CD;&#x8BBE;&#x5907;&#x63D0;&#x4F9B;&#x4E86;&#x4EE5;&#x6574;&#x5757;&#x4E3A;&#x7C92;&#x5EA6;&#x7684;&#x8BFB;&#x548C;&#x5199;&#x64CD;&#x4F5C;&#xFF0C;&#x4E00;&#x822C;&#x5BF9;&#x5E94;&#x5230;&#x771F;&#x5B9E;&#x7684;&#x7269;&#x7406;&#x8BBE;&#x5907;&#x662F;&#x90A3;&#x79CD;&#x786C;&#x76D8;&#x3002;&#x800C;&#x4E4B;&#x6240;&#x4EE5;&#x662F;&#x4EE5;&#x5757;&#x4E3A;&#x5355;&#x4F4D;&#x662F;&#x4E3A;&#x4E86;&#x52A0;&#x5FEB;&#x8BFB;&#x5199;&#x7684;&#x901F;&#x5EA6;&#xFF0C;&#x6BD5;&#x7ADF;&#x786C;&#x76D8;&#x7B49;&#x8BBE;&#x5907;&#x8FD8;&#x9700;&#x8981;&#x5BFB;&#x9053;&#x7B49;&#x7B49;&#x64CD;&#x4F5C;&#xFF0C;&#x4E00;&#x6B21;&#x6027;&#x8BFB;&#x53D6;&#x5F88;&#x5927;&#x7684;&#x4E00;&#x5757;&#x5C06;&#x4F1A;&#x8282;&#x7EA6;&#x5F88;&#x591A;&#x65F6;&#x95F4;&#x3002;</p>
<h3 id="&#x62BD;&#x8C61;&#x9A71;&#x52A8;">&#x62BD;&#x8C61;&#x9A71;&#x52A8;</h3>
<p>&#x5728;&#x5199;&#x5757;&#x8BBE;&#x5907;&#x9A71;&#x52A8;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x62BD;&#x8C61;&#x9A71;&#x52A8;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x4E5F;&#x65B9;&#x4FBF;&#x540E;&#x9762;&#x7F51;&#x7EDC;&#x8BBE;&#x5907;&#x7B49;&#x7684;&#x4ECB;&#x5165;&#x3002;</p>
<p></p><p class="code-label">os/src/drivers/driver.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-comment">/// &#x9A71;&#x52A8;&#x7C7B;&#x578B;</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">/// &#x76EE;&#x524D;&#x53EA;&#x6709;&#x5757;&#x8BBE;&#x5907;&#xFF0C;&#x53EF;&#x80FD;&#x8FD8;&#x6709;&#x7F51;&#x7EDC;&#x3001;GPU &#x8BBE;&#x5907;&#x7B49;</span>
<span class="hljs-meta">#[derive(Debug, Eq, PartialEq)]</span>
<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">DeviceType</span></span> {
    Block,
}

<span class="hljs-comment">/// &#x9A71;&#x52A8;&#x7684;&#x63A5;&#x53E3;</span>
<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Driver</span></span>: <span class="hljs-built_in">Send</span> + <span class="hljs-built_in">Sync</span> {
    <span class="hljs-comment">/// &#x8BBE;&#x5907;&#x7C7B;&#x578B;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">device_type</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; DeviceType;

    <span class="hljs-comment">/// &#x8BFB;&#x53D6;&#x67D0;&#x4E2A;&#x5757;&#x5230; buf &#x4E2D;&#xFF08;&#x5757;&#x8BBE;&#x5907;&#x63A5;&#x53E3;&#xFF09;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">read_block</span></span>(&amp;<span class="hljs-keyword">self</span>, _block_id: usize, _buf: &amp;<span class="hljs-keyword">mut</span> [<span class="hljs-keyword">u8</span>]) -&gt; <span class="hljs-keyword">bool</span> {
        <span class="hljs-built_in">unimplemented!</span>(<span class="hljs-string">&quot;not a block driver&quot;</span>)
    }

    <span class="hljs-comment">/// &#x5C06; buf &#x4E2D;&#x7684;&#x6570;&#x636E;&#x5199;&#x5165;&#x5757;&#x4E2D;&#xFF08;&#x5757;&#x8BBE;&#x5907;&#x63A5;&#x53E3;&#xFF09;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">write_block</span></span>(&amp;<span class="hljs-keyword">self</span>, _block_id: usize, _buf: &amp;[<span class="hljs-keyword">u8</span>]) -&gt; <span class="hljs-keyword">bool</span> {
        <span class="hljs-built_in">unimplemented!</span>(<span class="hljs-string">&quot;not a block driver&quot;</span>)
    }
}

lazy_static! {
    <span class="hljs-comment">/// &#x6240;&#x6709;&#x9A71;&#x52A8;</span>
    <span class="hljs-keyword">pub</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">ref</span> DRIVERS: RwLock&lt;<span class="hljs-built_in">Vec</span>&lt;Arc&lt;dyn Driver&gt;&gt;&gt; = RwLock::new(<span class="hljs-built_in">Vec</span>::new());
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6682;&#x65F6;&#x53EA;&#x6709;&#x5757;&#x8BBE;&#x5907;&#x8FD9;&#x4E2A;&#x79CD;&#x7C7B;&#xFF0C;&#x4E0D;&#x8FC7;&#x8FD9;&#x6837;&#x5199;&#x8FD8;&#x662F;&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x672A;&#x6765;&#x7684;&#x6269;&#x5C55;&#x3002;</p>
<h3 id="&#x62BD;&#x8C61;&#x5757;&#x8BBE;&#x5907;">&#x62BD;&#x8C61;&#x5757;&#x8BBE;&#x5907;</h3>
<p>&#x6709;&#x4E86;&#x9A71;&#x52A8;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x6211;&#x4EEC;&#x8FDB;&#x4E00;&#x6B65;&#x62BD;&#x8C61;&#x5757;&#x8BBE;&#x5907;&#xFF1A;</p>
<p></p><p class="code-label">os/src/drivers/block/mod.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-comment">/// &#x5757;&#x8BBE;&#x5907;&#x62BD;&#x8C61;&#xFF08;&#x9A71;&#x52A8;&#x7684;&#x5F15;&#x7528;&#xFF09;</span>
<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BlockDevice</span></span>(<span class="hljs-keyword">pub</span> Arc&lt;dyn Driver&gt;);

<span class="hljs-comment">/// &#x4E3A; [`BlockDevice`] &#x5B9E;&#x73B0; [`rcore-fs`] &#x4E2D; [`BlockDevice`] trait</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">/// &#x4F7F;&#x5F97;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x5757;&#x8BBE;&#x5907;&#x7684;&#x8BE5;&#x63A5;&#x53E3;&#x6765;&#x8BFB;&#x5199;</span>
<span class="hljs-keyword">impl</span> dev::BlockDevice <span class="hljs-keyword">for</span> BlockDevice {
    <span class="hljs-comment">/// &#x6BCF;&#x4E2A;&#x5757;&#x7684;&#x5927;&#x5C0F;&#xFF08;&#x53D6; 2 &#x7684;&#x5BF9;&#x6570;&#xFF09;</span>
    <span class="hljs-comment">///</span>
    <span class="hljs-comment">/// &#x8FD9;&#x91CC;&#x53D6; 512B &#x662F;&#x56E0;&#x4E3A; virtio &#x9A71;&#x52A8;&#x5BF9;&#x8BBE;&#x5907;&#x7684;&#x64CD;&#x4F5C;&#x7C92;&#x5EA6;&#x4E3A; 512B</span>
    <span class="hljs-keyword">const</span> BLOCK_SIZE_LOG2: <span class="hljs-keyword">u8</span> = <span class="hljs-number">9</span>;

    <span class="hljs-comment">/// &#x8BFB;&#x53D6;&#x67D0;&#x4E2A;&#x5757;&#x5230; buf &#x4E2D;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">read_at</span></span>(&amp;<span class="hljs-keyword">self</span>, block_id: usize, buf: &amp;<span class="hljs-keyword">mut</span> [<span class="hljs-keyword">u8</span>]) -&gt; dev::<span class="hljs-built_in">Result</span>&lt;()&gt; {
        <span class="hljs-keyword">match</span> <span class="hljs-keyword">self</span>.<span class="hljs-number">0</span>.read_block(block_id, buf) {
            <span class="hljs-literal">true</span> =&gt; <span class="hljs-literal">Ok</span>(()),
            <span class="hljs-literal">false</span> =&gt; <span class="hljs-literal">Err</span>(dev::DevError),
        }
    }

    <span class="hljs-comment">/// &#x5C06; buf &#x4E2D;&#x7684;&#x6570;&#x636E;&#x5199;&#x5165;&#x5757;&#x4E2D;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">write_at</span></span>(&amp;<span class="hljs-keyword">self</span>, block_id: usize, buf: &amp;[<span class="hljs-keyword">u8</span>]) -&gt; dev::<span class="hljs-built_in">Result</span>&lt;()&gt; {
        <span class="hljs-keyword">match</span> <span class="hljs-keyword">self</span>.<span class="hljs-number">0</span>.write_block(block_id, buf) {
            <span class="hljs-literal">true</span> =&gt; <span class="hljs-literal">Ok</span>(()),
            <span class="hljs-literal">false</span> =&gt; <span class="hljs-literal">Err</span>(dev::DevError),
        }
    }

    <span class="hljs-comment">/// &#x6267;&#x884C;&#x548C;&#x8BBE;&#x5907;&#x7684;&#x540C;&#x6B65;</span>
    <span class="hljs-comment">///</span>
    <span class="hljs-comment">/// &#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x5168;&#x90E8;&#x4E3A;&#x963B;&#x585E; I/O &#x6240;&#x4EE5;&#x4E0D;&#x5B58;&#x5728;&#x540C;&#x6B65;&#x7684;&#x95EE;&#x9898;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">sync</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; dev::<span class="hljs-built_in">Result</span>&lt;()&gt; {
        <span class="hljs-literal">Ok</span>(())
    }
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6240;&#x8C13;&#x7684; <code>BlockDevice</code> &#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E00;&#x4E2A; <code>Driver</code> &#x7684;&#x5F15;&#x7528;&#x3002;&#x800C;&#x4E14;&#x5229;&#x7528; rcore-fs &#x4E2D;&#x63D0;&#x4F9B;&#x7684; <code>BlockDevice</code> trait &#x5B9E;&#x73B0;&#x4E86;&#x4E3A;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x5BF9;&#x4E0A;&#x4F20;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x7684;&#x8FDE;&#x63A5;&#x3002;</p>
<h3 id="virtio-blk-&#x5757;&#x8BBE;&#x5907;&#x9A71;&#x52A8;">virtio-blk &#x5757;&#x8BBE;&#x5907;&#x9A71;&#x52A8;</h3>
<p>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x5B9E;&#x73B0; virtio-blk &#x7684;&#x9A71;&#x52A8;&#xFF08;&#x4E3B;&#x8981;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x73B0;&#x6210;&#x7684;&#x5E93;&#x5B8C;&#x6210;&#xFF09;&#xFF1A;</p>
<p></p><p class="code-label">os/src/drivers/block/virtio_blk.rs</p><p></p>
<pre><code class="lang-rust"><span class="hljs-comment">/// virtio &#x534F;&#x8BAE;&#x7684;&#x5757;&#x8BBE;&#x5907;&#x9A71;&#x52A8;</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">VirtIOBlkDriver</span></span>(Mutex&lt;VirtIOBlk&lt;<span class="hljs-symbol">&apos;static</span>&gt;&gt;);

<span class="hljs-comment">/// &#x4E3A; [`VirtIOBlkDriver`] &#x5B9E;&#x73B0; [`Driver`] trait</span>
<span class="hljs-comment">///</span>
<span class="hljs-comment">/// &#x8C03;&#x7528;&#x4E86; [`virtio_drivers`] &#x5E93;&#xFF0C;&#x5176;&#x4E2D;&#x89C4;&#x5B9A;&#x7684;&#x5757;&#x5927;&#x5C0F;&#x4E3A; 512B</span>
<span class="hljs-keyword">impl</span> Driver <span class="hljs-keyword">for</span> VirtIOBlkDriver {
    <span class="hljs-comment">/// &#x8BBE;&#x5907;&#x7C7B;&#x578B;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">device_type</span></span>(&amp;<span class="hljs-keyword">self</span>) -&gt; DeviceType {
        DeviceType::Block
    }

    <span class="hljs-comment">/// &#x8BFB;&#x53D6;&#x67D0;&#x4E2A;&#x5757;&#x5230; buf &#x4E2D;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">read_block</span></span>(&amp;<span class="hljs-keyword">self</span>, block_id: usize, buf: &amp;<span class="hljs-keyword">mut</span> [<span class="hljs-keyword">u8</span>]) -&gt; <span class="hljs-keyword">bool</span> {
        <span class="hljs-keyword">self</span>.<span class="hljs-number">0</span>.lock().read_block(block_id, buf).is_ok()
    }

    <span class="hljs-comment">/// &#x5C06; buf &#x4E2D;&#x7684;&#x6570;&#x636E;&#x5199;&#x5165;&#x5757;&#x4E2D;</span>
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">write_block</span></span>(&amp;<span class="hljs-keyword">self</span>, block_id: usize, buf: &amp;[<span class="hljs-keyword">u8</span>]) -&gt; <span class="hljs-keyword">bool</span> {
        <span class="hljs-keyword">self</span>.<span class="hljs-number">0</span>.lock().write_block(block_id, buf).is_ok()
    }
}

<span class="hljs-comment">/// &#x5C06;&#x4ECE;&#x8BBE;&#x5907;&#x6811;&#x4E2D;&#x8BFB;&#x53D6;&#x51FA;&#x7684;&#x8BBE;&#x5907;&#x4FE1;&#x606F;&#x653E;&#x5230; [`static@DRIVERS`] &#x4E2D;</span>
<span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">add_driver</span></span>(header: &amp;<span class="hljs-symbol">&apos;static</span> <span class="hljs-keyword">mut</span> VirtIOHeader) {
    <span class="hljs-keyword">let</span> virtio_blk = VirtIOBlk::new(header).expect(<span class="hljs-string">&quot;failed to init blk driver&quot;</span>);
    <span class="hljs-keyword">let</span> driver = Arc::new(VirtIOBlkDriver(Mutex::new(virtio_blk)));
    DRIVERS.write().push(driver.clone());
}
</code></pre>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x73B0;&#x5728;&#x7684;&#x903B;&#x8F91;&#x600E;&#x4E48;&#x770B;&#x90FD;&#x4E0D;&#x50CF;&#x662F;&#x4E4B;&#x524D;&#x63D0;&#x5230;&#x7684;<strong>&#x5F02;&#x6B65; DMA + IRQ &#x4E2D;&#x65AD;</strong>&#x7684;&#x9AD8;&#x7EA7; I/O &#x64CD;&#x4F5C;&#x6280;&#x672F;&#xFF0C;&#x800C;&#x66F4;&#x50CF;&#x662F;&#x963B;&#x585E;&#x7684;&#x8BFB;&#x53D6;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#x7684;&#x786E;&#x662F;&#x963B;&#x585E;&#x7684;&#x8BFB;&#x53D6;&#xFF0C;&#x76EE;&#x524D; virtio-drivers &#x5E93;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x867D;&#x7136;&#x8C03;&#x7528;&#x4E86; DMA&#xFF0C;&#x4F46;&#x662F;&#x8FD4;&#x56DE;&#x65F6;&#x8FD8;&#x662F;&#x963B;&#x585E;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x6211;&#x4EEC;&#x8FD9;&#x91CC;&#x4E3A;&#x4E86;&#x7B80;&#x5316;&#x4E5F;&#x6CA1;&#x6709;&#x8BBE;&#x8BA1; IRQ &#x7684;&#x54CD;&#x5E94;&#x673A;&#x5236;&#x3002;</p>
<!-- TODO 写 IRQ 响应 -->
<h3 id="&#x5C0F;&#x7ED3;">&#x5C0F;&#x7ED3;</h3>
<p>&#x81F3;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5B8C;&#x6210;&#x4E86;&#x5168;&#x90E8;&#x7684;&#x9A71;&#x52A8;&#x903B;&#x8F91;&#xFF0C;&#x6211;&#x4EEC;&#x603B;&#x7ED3;&#x4E00;&#x4E0B;&#x76EE;&#x524D;&#x7684;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<div align="center"> <img src="../pics/design.png" style="zoom:40%;"> </div>

<p>&#x5176;&#x4E2D; <code>Driver</code> &#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x6838;&#x5FC3; trait &#x4E3A;&#x4E0A;&#x63D0;&#x4F9B;&#x5B9E;&#x73B0;&#xFF0C;&#x4E0A;&#x5C42;&#x4E5F;&#x5C31;&#x662F; <code>Driver</code> &#x7684;&#x4F7F;&#x7528;&#x4FA7;&#xFF08;&#x8BBE;&#x5907;&#x7684;&#x62BD;&#x8C61;&#xFF09;&#xFF0C;&#x800C;&#x4E0B;&#x5C42;&#x5219;&#x662F; <code>Driver</code> &#x7684;&#x5B9E;&#x73B0;&#x4FA7;&#xFF08;&#x8BBE;&#x5907;&#x7684;&#x5B9E;&#x73B0;&#xFF09;&#x3002;&#x800C;&#x4E0B;&#x4E00;&#x4E2A;&#x5C0F;&#x8282;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5229;&#x7528;&#x8FD9;&#x4E9B;&#x9A71;&#x52A8;&#x6765;&#x5B9E;&#x73B0;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x3002;</p>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css"></p>
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
    var gitalk = new Gitalk({
        clientID: "5084c46751a4b2f7cd4d",
        clientSecret: "3904f62d33aef24f19480fedc836a48b31d532e9",
        repo: "rCore-Tutorial-deploy",
        owner: "rcore-os",
        admin: [
            "chyyuu",
            "wangrunji0408",
            "xy-plus",
            "wyfcyx",
            "LyricZhao",
            "Tuyixiang"
        ],
        id: location.pathname.replace('/rCore-Tutorial-deploy', '').replace('.html', ''),
        distractionFreeMode: false
    });
    gitalk.render("gitalk-container");
</script></div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part-2.html" class="navigation navigation-prev " aria-label="Previous page: virtio">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part-4.html" class="navigation navigation-next " aria-label="Next page: 文件系统">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"驱动和块设备驱动","level":"3.6.4","depth":2,"next":{"title":"文件系统","level":"3.6.5","depth":2,"path":"docs/lab-5/guide/part-4.md","ref":"docs/lab-5/guide/part-4.md","articles":[]},"previous":{"title":"virtio","level":"3.6.3","depth":2,"path":"docs/lab-5/guide/part-2.md","ref":"docs/lab-5/guide/part-2.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab-5/guide/part-3.md","mtime":"2020-10-01T11:39:40.519Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-01T11:40:38.595Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

